package cn.edu.buaa.nlsde.grid.DatabaseConnect;

import java.sql.*;
import javax.sql.*;
import javax.naming.*;

public class DBPool
{
	Context ctx=null;
	DataSource ds=null;
	Connection conn=null;
	Statement stat=null;
	
	public DBPool(String jndiName)
	{
		try
		{
			ctx=new InitialContext();
			ds=(DataSource)ctx.lookup(jndiName);
			conn=ds.getConnection();
		}
		catch(NamingException e)
		{
			e.printStackTrace();
		}
		catch(SQLException e)
		{
			e.printStackTrace();
		}	
	}
	
    public Statement connect() throws SQLException
    {
    	stat=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
    	System.out.println("Succes estblishing the Connection...");
    	return stat;
    }
    
    public void close() throws SQLException
    {
    	System.out.println("Trying to close...");
    	stat.close();
    	conn.close();
    	System.out.println("Database connection has been put into pool!");
    }
}
